package com.springbootbase.springbootproject.utils;

import com.springbootbase.springbootproject.pojo.Recommendationcontact;

import java.util.*;

public class recommend {

    private RulesRecommend baseContent ;

    private List<Recommendationcontact> recommendationLists; // 假设已经有推荐列表数据

//    学生发给导师的  {学生id，【导师id】}
    public Map<Long, List<Long>> studentToteacher = new HashMap<>();
//    记录导师收到的学生
    public Map<Long, List<Long>> teacherAccept = new HashMap<>();

//    最后获得的用户相似度
    public Map<Long, Double> sortedSimilarity = new LinkedHashMap<>();
//    最后推荐的列表
    public Map<Long, Double> finalRecommend = new LinkedHashMap<>();

    public recommend(List<Recommendationcontact> recommendationLists,String type) {
        this.recommendationLists = recommendationLists;
        handleInfo(type);
    }

    public void handleInfo(String type){
        if (type.equals("student")){
            for (Recommendationcontact i : recommendationLists){
                if (Objects.equals(i.getInitiator(), "student")){
                    if (studentToteacher != null && studentToteacher.containsKey(i.getStudentId())) {
                        studentToteacher.get(i.getStudentId()).add(i.getTeacherId());
                    }else {
                        List<Long> a = new ArrayList<>();
                        a.add(i.getTeacherId());
                        studentToteacher.put(i.getStudentId(),a);
                    }
                    if (teacherAccept != null && teacherAccept.containsKey(i.getTeacherId())) {
                        teacherAccept.get(i.getTeacherId()).add(i.getStudentId());
                    }else {
                        List<Long> a = new ArrayList<>();
                        a.add(i.getStudentId());
                        teacherAccept.put(i.getTeacherId(),a);
                    }
                }
            }
        }else{
            for (Recommendationcontact i : recommendationLists){
                if (Objects.equals(i.getInitiator(), "teacher")){
                    if (studentToteacher!=null && studentToteacher.containsKey(i.getTeacherId())){
                        studentToteacher.get(i.getTeacherId()).add(i.getStudentId());
                    }else{
                        List<Long> a = new ArrayList<>();
                        a.add(i.getStudentId());
                        studentToteacher.put(i.getTeacherId(),a);
                    }
                    if (teacherAccept!=null && teacherAccept.containsKey(i.getStudentId())){
                        teacherAccept.get(i.getStudentId()).add(i.getTeacherId());
                    }else{
                        List<Long> a = new ArrayList<>();
                        a.add(i.getTeacherId());
                        teacherAccept.put(i.getStudentId(),a);
                    }
                }
            }
        }

    }


   public Map<Long, Double> userBasedRecommendForStudent(Long id){
        Map<Long,Integer> commonTeacher = new HashMap<>();
        Map<Long, Double> finalSimilarity = new HashMap<>();
//        如果有这个值 就代表该学生曾经联系过导师，有记录才好推荐相似学生
        if (studentToteacher.containsKey(id)){
            List<Long> teacherId = studentToteacher.get(id);
//            只遍历学生联系过的导师列表，因为超出这个范围分子为0
            for (int i = 0; i < teacherId.size(); i++) {
                List<Long> studentId  = teacherAccept.get(teacherId.get(i));
                for (int j = 0; j < studentId.size(); j++) {
                    if (id!=studentId.get(j)){
                        if (!commonTeacher.containsKey(studentId.get(j))){
                            commonTeacher.put(studentId.get(j),1);
                        }else {
                            commonTeacher.put(studentId.get(j),commonTeacher.get(studentId.get(j))+1);
                        }
                    }
                }
            }
            if (!commonTeacher.isEmpty()){
                for (Long lid : commonTeacher.keySet()){
                    int a = studentToteacher.get(id).size();
                    int b = studentToteacher.get(lid).size();
                    finalSimilarity.put(lid,commonTeacher.get(lid)/Math.sqrt(a*b));
                }
                List<Map.Entry<Long, Double>> entries = new ArrayList<>(finalSimilarity.entrySet());

                // 根据值进行降序排序
                entries.sort((e1, e2) -> Double.compare(e2.getValue(), e1.getValue()));

                // 输出排序后的结果

                for (Map.Entry<Long, Double> entry : entries) {
                    sortedSimilarity.put(entry.getKey(), entry.getValue());
                }

                getFinalRecommend(id);
            }

        }

       return finalRecommend;
   }


   public void getFinalRecommend(Long id){
        List<Long> deleteTeacher = studentToteacher.get(id);
        Map<Long,Double> recommendScore = new HashMap<>();
       for (Map.Entry<Long, Double> entry : sortedSimilarity.entrySet()) {
           Long studentId = entry.getKey();
           List<Long> teacher = studentToteacher.get(studentId);
           for (Long i : teacher){
//               如果没包含这个导师
               if (!deleteTeacher.contains(i)){
                   if (!recommendScore.containsKey(i)){
                       recommendScore.put(i,entry.getValue());
                   }else{
                       Double d = recommendScore.get(i);
                       recommendScore.put(i,d+entry.getValue());
                   }
               }
           }
       }
       List<Map.Entry<Long, Double>> entries = new ArrayList<>(recommendScore.entrySet());
       // 根据值进行降序排序
       entries.sort((e1, e2) -> Double.compare(e2.getValue(), e1.getValue()));


       // 输出排序后的结果
       for (Map.Entry<Long, Double> entry : entries) {
           finalRecommend.put(entry.getKey(), entry.getValue());
       }

   }




}
